<template>
  <el-dialog
    :title="isEdit ? '编辑教师' : '新增教师'"
    v-model="dialogVisible"
    width="500px"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="80px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入教师姓名" />
      </el-form-item>
      
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入手机号" />
      </el-form-item>
      
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" placeholder="请输入邮箱" />
      </el-form-item>
      
      <el-form-item label="科目" prop="subject">
        <el-input v-model="form.subject" placeholder="请输入任教科目" />
      </el-form-item>

      <el-form-item label="状态">
        <el-switch
          v-model="form.status"
          :active-value="'active'"
          :inactive-value="'inactive'"
          active-text="在职"
          inactive-text="离职"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleConfirm" :loading="loading">
        {{ isEdit ? '保存' : '创建' }}
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref } from 'vue'
import { teacherService } from '../../services/teacher'
import { showSuccess, showError } from '../../utils/message'

const dialogVisible = ref(false)
const loading = ref(false)
const formRef = ref(null)
const isEdit = ref(false)

const form = ref({
  name: '',
  phone: '',
  email: '',
  subject: '',
  status: 'active'
})

const rules = {
  name: [{ required: true, message: '请输入教师姓名', trigger: 'blur' }],
  phone: [{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }],
  email: [{ type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }],
  subject: [{ required: true, message: '请输入任教科目', trigger: 'blur' }]
}

const resetForm = () => {
  form.value = {
    name: '',
    phone: '',
    email: '',
    subject: '',
    status: 'active'
  }
  formRef.value?.resetFields()
}

const open = (data = null) => {
  isEdit.value = !!data
  if (data) {
    form.value = { ...data }
  } else {
    resetForm()
  }
  dialogVisible.value = true
}

const handleConfirm = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    loading.value = true
    
    let result
    if (isEdit.value) {
      result = await teacherService.updateTeacher(form.value.id, form.value)
      showSuccess('修改成功')
    } else {
      result = await teacherService.createTeacher(form.value)
      showSuccess('创建成功')
    }
    
    if (result) {
      dialogVisible.value = false
      emit('success')
      resetForm()
    }
  } catch (error) {
    showError(error?.message || '操作失败')
  } finally {
    loading.value = false
  }
}

const handleCancel = () => {
  dialogVisible.value = false
  resetForm()
}

const emit = defineEmits(['success'])

defineExpose({
  open
})
</script>
